package com.dudu.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dudu.bean.IfControlRuleBean;

import core.CoreDao;
import core.CoreDaoInvoke;
import core.db.DBConnectionUtil;
import net.sf.json.JSONObject;

/**
 * 信控规则数据库操作类
 */
public class InfoControlRuleDao {

	private static CoreDao coreDao = CoreDaoInvoke.getInstance();

	// 日志输出
	private static final Logger log = Logger.getLogger(InfoControlRuleDao.class.getSimpleName());

	/**
	 * 查询信控规则配置
	 * 
	 * @param ifControl
	 *            信控规则条件
	 * @param includeCommonRule
	 *            是否包含通用规则（所有app均适用）
	 * @return
	 */
	public List<JSONObject> queryIfControlRules(IfControlRuleBean ifControl, boolean includeCommonRule) {
		List<JSONObject> list = null;
		// 数据库连接
		Connection con = null;
		List parameter = new ArrayList();
		parameter.add(ifControl.getAbilityType());
		parameter.add(ifControl.getAppKey());
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			String[] appKey = ifControl.getAppKey().split(",");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					"select id,send_count,check_numbers,check_period,displaynum_record from t_infocontrol_rules where ability_type=? and app_key in(?");
			if (includeCommonRule) {
				sql.append(",?)");
				parameter.add("-1");
			} else {
				sql.append(")");
			}
			sql.append(" and starttime<=? and endtime>=? and use_flag=?");
			parameter.add(ifControl.getStartTime());
			parameter.add(ifControl.getEndTime());
			parameter.add(ifControl.getUseFlag());
			// 查询获取结果
			list = coreDao.serviceJsonSql(con, sql.toString(), parameter.toArray());
		} catch (Exception ex) {
			log.error("查询信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return list;
	}

	public List<JSONObject> queryALLIfControlRules(String pageSql) {
		List<JSONObject> list = null;
		// 数据库连接
		Connection con = null;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					"select id,ability_type,app_key,send_count,check_numbers,check_period,displaynum_record,use_flag,starttime,endtime from t_infocontrol_rules ");
			sql.append(pageSql);
			// 查询获取结果
			list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] {});
		} catch (Exception ex) {
			log.error("查询信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return list;
	}

	public int queryALLIfControlRulesCount() {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("select count(1) as totalNumberPool from t_infocontrol_rules ");
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] {});
			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalNumberPool");
		} catch (Exception ex) {
			log.error("查询信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int createControlRule(IfControlRuleBean ruleBean) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" insert into t_infocontrol_rules(ability_type,app_key,send_count,displaynum_record,starttime,endtime,check_numbers,check_period,create_time,use_flag,create_user) ");
			sql.append(" values(?,?,?,?,?,?,?,?,SYSDATE(),?,?) ");
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { ruleBean.getAbilityType(), ruleBean.getAppKey(), ruleBean.getMaxSendCount(),
							ruleBean.getDisplaynumRecord(), ruleBean.getStartTime(), ruleBean.getEndTime(),
							ruleBean.getCheckNumbers(), ruleBean.getCheckPeriod(), ruleBean.getUseFlag(),
							ruleBean.getCreateUser() });
			JSONObject obj = list.get(0);
			result = (Integer) obj.get("result");
		} catch (Exception ex) {
			log.error("新建信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int updateControlRule(IfControlRuleBean ruleBean) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			String sql = "update t_infocontrol_rules set send_count=?,displaynum_record=?,starttime=?,endtime=?,check_numbers=?,check_period=?,update_user=? where id=? ";
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql,
					new Object[] { ruleBean.getMaxSendCount(), ruleBean.getDisplaynumRecord(), ruleBean.getStartTime(),
							ruleBean.getEndTime(), ruleBean.getCheckNumbers(), ruleBean.getCheckPeriod(),
							ruleBean.getUpdateUser(), ruleBean.getId() });
			JSONObject obj = list.get(0);
			result = (Integer) obj.get("result");
		} catch (Exception ex) {
			log.error("修改信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int updateControlRuleStatus(IfControlRuleBean ruleBean) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			String sql = "update t_infocontrol_rules set use_flag=?,update_user=? where id=? ";
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql,
					new Object[] { ruleBean.getUseFlag(), ruleBean.getUpdateUser(), ruleBean.getId() });
			JSONObject obj = list.get(0);
			result = (Integer) obj.get("result");
		} catch (Exception ex) {
			log.error("修改信控规则配置发生异常", ex);
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

}
